<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class transaksi_pembelian extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('m_transaksi_pembelian');
		$this->load->library('breadcrumb');	
	}

	public function index(){
		$this->rule->type('R');
		$this->load->library('paging');

		$config['sql'] = $this->m_transaksi_pembelian->get_all();
		$config['per_page'] = 6;
		$data = $this->paging->create_pagination($config);
		$judul = "Transaksi Pembelian";
		$data['judul'] = $judul;
		$this->layout->set_title($judul);
		$this->layout->set_desc("lorem ipsum");

		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
		$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
		$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
		$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');

		#breadcrumbs
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Transaksi', site_url('transaksi_pembelian#'));
		$this->breadcrumb->add_crumb($judul);

		$this->layout->theme('apotek', 'transaksi_pembelian/index', $data);		
	}

	public function search()
	{
		if($this->input->post() != ""){
			$input = $this->input->post();

			$this->rule->type('R');
			$this->load->library('paging');
						
			if($input['filter'] == "by_waktu"){
				$keyword = array($input['waktu_awal'], $input['waktu_akhir']);				
			}else if($input['filter'] == "by_petugas"){
				$keyword = $input['petugas'];
			}else if($input['filter'] == "by_kelompok"){
				$keyword = $input['kelompok'];
			}else if($input['filter'] == "by_obat"){
				$keyword = $input['obat'];
			}

			$config['sql'] = $this->m_transaksi_pembelian->search($input['filter'], $keyword);	
			$config['per_page'] = 6;
			$data = $this->paging->create_pagination($config);

			$judul = "Cari Data Pembelian";
			$data['judul'] = $judul;
			$this->layout->set_title($judul);
			$this->layout->set_desc("lorem ipsum");

			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
			$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
			$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
			$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');

		#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Transaksi', site_url('transaksi_pembelian'));
			$this->breadcrumb->add_crumb($judul);

			$this->layout->theme('apotek', 'transaksi_pembelian/index', $data);		
		}else{
			show_404();
		}
	}

	public function add(){
		$this->rule->type('C');

		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_transaksi_pembelian->store($input);
			redirect('transaksi_pembelian', 'refresh');
		}else{
			$judul = 'Tambah Detail Beli';
			$data['judul']  = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');
			$this->layout->set_include('js', 'themes/apotek/js/olide-custom.js');

	    #breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Obat', site_url('obat_data#'));
			$this->breadcrumb->add_crumb('Transaksi Pemebelian', site_url('transaksi_pembelian'));
			$this->breadcrumb->add_crumb($judul);

			$data['obat'] = $this->m_transaksi_pembelian->get_obat();

			$this->layout->theme('apotek','transaksi_pembelian/add', $data);
		}
	}

	public function edit($id){
		$this->rule->type('U');

		if (isset($_POST) && !empty($_POST)) {
			$input  = $this->input->post();
			$this->m_transaksi_pembelian->update($input, $id);

			redirect('transaksi_pembelian', 'refresh');
		}else{
			$this->load->helper('cek_id');
			if(!cek_id($id, 'KdBeli', 'beli')){
				show_404();
			}

			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');

			$judul = 'Edit Transaksi Pembelian';
			$data['judul'] = $judul;

			$this->layout->set_title($judul);
			$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

  		#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Transaksi Pembelian', site_url('transaksi_pembelian'));
			$this->breadcrumb->add_crumb($judul);

			$data['data'] = $this->m_transaksi_pembelian->get_by_id($id);
			$data['detail_beli'] = $this->m_transaksi_pembelian->get_detail_byid($id);
			$data['obat'] = $this->m_transaksi_pembelian->get_obat();

			$this->layout->theme('apotek','transaksi_pembelian/edit', $data);
		}
	}

	public function delete($id){
		$this->rule->type('D');

		if($id != ""){
			$this->load->helper('cek_id');
			if(!cek_id($id, 'KdBeli', 'beli')){
				show_404();
			}
			$this->m_transaksi_pembelian->destroy($id);
			redirect('transaksi_pembelian', 'refresh');	
		}else{
			show_404();
		}
	}

	public function excel(){
		$this->rule->type('R');
		
		#load our new PHPExcel library
		$this->load->library('excel');
		#activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		#name the worksheet
		$this->excel->getActiveSheet()->setTitle('Data Transaksi Pembelian');
		#set cell A1 content with some text
		$this->excel->getActiveSheet()->setCellValue('A1', 'Data Transaksi Pembelian '.site_title());

		$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
		$this->excel->getActiveSheet()->setCellValue('B2', 'Tanggal');
		$this->excel->getActiveSheet()->setCellValue('C2', 'User');
		$this->excel->getActiveSheet()->setCellValue('D2', 'List Obat');
		#change the font size
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		#make the font become bold
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		#merge cell A1 until D1
		$this->excel->getActiveSheet()->mergeCells('A1:D1');
		#set aligment to center for that merged cell (A1 to D1)
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		#set style
		$TitleStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // hijau tua
				),
			'font'  => array(
				'bold'  => true,
  			//'color' => array('rgb' => '#666666'),
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$HeaderStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // hijau
				),
			'font'  => array(
				'bold'  => true,
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$ContentStyle = array(
			'borders' => array(
				'allborders' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN
					)
				),
			'fill' => array(
				'type' => PHPExcel_Style_Fill::FILL_SOLID,
            //'color' => array('rgb' => 'FFFFFF') // kuning
				),
			'font'  => array(
				'size'  => 12,
				'name'  => 'calibri'
				)
			);

		$this->excel->getActiveSheet()->getStyle('A1:D1')->applyFromArray($TitleStyle);
		$this->excel->getActiveSheet()->getStyle('A2:D2')->applyFromArray($HeaderStyle);
		$this->excel->getActiveSheet()->getStyle('D')->getAlignment()->setWrapText(true);
		$this->excel->getActiveSheet()->getStyle('A:C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT)->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
		
		//isi
		$list_data = $this->m_transaksi_pembelian->export();

		$cols = 3;
		$num = 1;
		foreach($list_data as $row){
			$col = $cols++;
			$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
			
			$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['Tanggal']);
			$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['nickname']);
			
			$this->db->join('obat', 'obat.KdObat = detail_beli.KdObat');	
			$this->db->where('KdBeli', $row['KdBeli']);
			$q_detail = $this->db->get("detail_beli")->result_array();
			$obat_list = "";
			foreach($q_detail as $row2){
				$obat_list .= "$row2[NmObat]\n";
			}
			$obat_list = substr($obat_list, 0, strlen($obat_list)-1);
			$this->excel->getActiveSheet()->setCellValue('D' . $col, $obat_list);
		}

		#warna
		$cols2 = 3;
		foreach($list_data as $row){
			$col2 = $cols2++;
			$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
		}


		$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);


		$filename='data transaksi pembelian.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}
}

/* End of file transaksi_pembelian.php */
/* Location: ./application/controllers/transaksi_pembelian.php */